package cn.iocoder.yudao.module.yyzx.controller.admin.rooms;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.rooms.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.rooms.RoomsDO;
import cn.iocoder.yudao.module.yyzx.service.rooms.RoomsService;

@Tag(name = "管理后台 - 房间信息")
@RestController
@RequestMapping("/yyzx/rooms")
@Validated
public class RoomsController {

    @Resource
    private RoomsService roomsService;

    @PostMapping("/create")
    @Operation(summary = "创建房间信息")
    @PreAuthorize("@ss.hasPermission('yyzx:rooms:create')")
    public CommonResult<Integer> createRooms(@Valid @RequestBody RoomsSaveReqVO createReqVO) {
        return success(roomsService.createRooms(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新房间信息")
    @PreAuthorize("@ss.hasPermission('yyzx:rooms:update')")
    public CommonResult<Boolean> updateRooms(@Valid @RequestBody RoomsSaveReqVO updateReqVO) {
        roomsService.updateRooms(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除房间信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:rooms:delete')")
    public CommonResult<Boolean> deleteRooms(@RequestParam("id") Integer id) {
        roomsService.deleteRooms(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得房间信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:rooms:query')")
    public CommonResult<RoomsRespVO> getRooms(@RequestParam("id") Integer id) {
        RoomsDO rooms = roomsService.getRooms(id);
        return success(BeanUtils.toBean(rooms, RoomsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得房间信息分页")
    @PreAuthorize("@ss.hasPermission('yyzx:rooms:query')")
    public CommonResult<PageResult<RoomsRespVO>> getRoomsPage(@Valid RoomsPageReqVO pageReqVO) {
        PageResult<RoomsDO> pageResult = roomsService.getRoomsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RoomsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出房间信息 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:rooms:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoomsExcel(@Valid RoomsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RoomsDO> list = roomsService.getRoomsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "房间信息.xls", "数据", RoomsRespVO.class,
                        BeanUtils.toBean(list, RoomsRespVO.class));
    }

}